/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.notification.control;

import com.aljfce.business.entity.Event;
import com.aljfce.business.entity.Notification;
import com.aljfce.business.entity.User;
import com.aljfce.business.mail.control.MailManager;
import com.aljfce.business.notification.exception.NotificationNotFoundException;
import com.aljfce.business.utils.LoggerProducer;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 *
 * @author Jacopo Ferrigno <jacopo.ferrigno@gmail.com>
 */
@RunWith(Arquillian.class)
public class NotificationManagerIT {

    @Inject
    NotificationManager cut;

    @PersistenceContext
    EntityManager em;

    @Resource
    private UserTransaction utx;

    private Notification notification;
    private User user;
    private Event event;

    @Deployment
    public static WebArchive createArchiveAndDeploy() {

        WebArchive wa = ShrinkWrap.create(WebArchive.class)
                .addClass(NotificationManager.class)
                .addPackage(Notification.class.getPackage())
                .addPackage(LoggerProducer.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
        //System.out.println("ARCHIVE:\n" + wa.toString(true));
        return wa;
    }

    @Before
    public void init() throws Exception {
        utx.begin();
        this.user = new User("t@t.com", "t", "t", "t", "USER");
        em.persist(this.user);
        utx.commit();

        utx.begin();
        this.event = new Event();
        this.event.setCreator(user);
        this.event.setStartingDate(new Date());
        this.event.setEndingTime(new Date());
        this.event.setOutdoor(true);
        em.persist(event);
        utx.commit();
    }

    @After
    public void cleanup() throws Exception {
        utx.begin();
        event = em.find(Event.class, event.getId());
        em.remove(event);
        utx.commit();

        utx.begin();
        user = em.find(User.class, user.getEmail());
        em.remove(user);
        utx.commit();
    }

    @Test
    @InSequence(1)
    public void notificationManagerShouldBeInjected() {
        assertNotNull(cut);
    }

    @Test
    @InSequence(2)
    public void entityManagerInjected() {
        assertNotNull(em);
    }

    @Test
    @InSequence(3)
    public void newNotificationShouldPersistNotification() throws Exception {
        this.notification = cut.newNotification(Notification.TYPE.INVITATION_NOTIFICATION, this.user, this.event);
        assertNotNull(em.find(Notification.class, notification.getId()));

    }

    @Test
    @InSequence(4)
    public void deleteNotificationShouldRemoveEntity() throws Exception {
        this.notification = cut.newNotification(Notification.TYPE.INVITATION_NOTIFICATION, this.user, this.event);
        cut.deleteNotification(notification);
        assertNull(this.em.find(Notification.class, notification.getId()));
    }

    @Test(expected = NotificationNotFoundException.class)
    @InSequence(5)
    public void deleteNotificationShouldThrowExceptionIfEntityNotPersisted() throws Exception {
        Notification n = new Notification(Notification.TYPE.INVITATION_NOTIFICATION, false, new Date());
        n.setId(0);
        cut.deleteNotification(n);
    }

}
